package org.example.B_100;

import java.util.*;

public class OptionalCourse {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input1 = scanner.nextLine();
        String input2 = scanner.nextLine();

        getResult(input1,input2);
    }

    public static void getResult(String s1,String s2){
        //定义存储学生的哈希
        HashMap<String,Student> students = new HashMap<>();

        //解析输入,将相关数据存储在student哈希中
        divide(students,s1,1);
        divide(students,s2,2);

        //筛选出有两门成绩的学生
        Student[] suits = students.values().stream()
                .filter(stu -> stu.score1 != -1 && stu.score2 != -1)
                .toArray(Student[]::new);

        //若不存在两门课程都有成绩的学习，则返回空
        if (suits.length == 0){
            System.out.println("NULL");
            return;
        }

        //对存在两门成绩的学生进行排序
        HashMap<String, ArrayList<Student>> ans = new HashMap<>();
        for (Student stu : suits){
            //不存在这个班级，就创建班级集合
            ans.putIfAbsent(stu.cid,new ArrayList<>());
            //将学生放到对应班级集合中
            ans.get(stu.cid).add(stu);
        }

        //按照班级和学生成绩来排序
        ans.keySet().stream()
                .sorted(String::compareTo)
                .forEach(
                        cid->{
                            System.out.println(cid);
                            ArrayList<Student> studs = ans.get(cid);
                            studs.sort(
                                    (a,b)->
                                        a.getSumScore() != b.getSumScore() ? b.getSumScore() - a.getSumScore() : a.sid.compareTo(b.sid)

                            );

                            StringJoiner sj = new StringJoiner(";");
                            for (Student stud : studs) sj.add(stud.sid);
                            System.out.println(sj);
                        }
                );






    }

    //解析输入函数
    public static void divide(HashMap<String,Student> students,String s,int courseId){
        for(String sub : s.split(";")){
            String[] tmp = sub.split(",");
            String sid = tmp[0];
            String cid = sid.substring(0,5);
            int score = Integer.parseInt(tmp[1]);

            //若不存在该学生对象
            students.putIfAbsent(sid,new Student());

            //初始化学生对象(已经存在对象)
            Student stu = students.get(sid);


            //传入班级等参数
            stu.sid = sid;
            stu.cid = cid;

            //传入成绩
            if (courseId ==1) stu.score1 = score;
            else stu.score2 = score;
        }
    }

    static class Student{
        String sid; //学号
        String cid; //班号

        int score1 = -1;
        int score2 = -2;

        //求得学生课程总分
        public int getSumScore(){
            return this.score1+this.score2;
        }
    }

}
